﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Security.AccessControl;

namespace System.Peppers.Wmi
{
    /// <summary>
    /// The Win32_SecurityDescriptor abstract WMI class represents a SECURITY_DESCRIPTOR structure. A security descriptor contains the security information for a securable object. The Owner and Group properties identify the owner and primary group for the object. It can also contain a discretionary access control list (DACL) that controls access to the object and a system access control list (SACL) that controls the logging of attempts to access the object.
    /// </summary>
    public sealed class WmiSecurityDescriptor : WmiWrapper
    {
        #region Static
        private static class WmiProperties
        {
            public const string ControlFlags = "ControlFlags",
                DACL = "DACL",
                Group = "Group",
                Owner = "Owner",
                SACL = "SACL";
        }
        #endregion

        /// <summary>
        /// Creates a new instance
        /// </summary>
        public WmiSecurityDescriptor()
            : this(new ManagementObject(WmiClasses.Win32_Trustee))
        { }

        internal WmiSecurityDescriptor(ManagementObject wmiObject)
            : base(wmiObject)
        { }

        /// <summary>
        /// Control bits that qualify the meaning of a security descriptor (SD) or its individual members. See the Remarks section of this topic for information about setting the ControlFlags value. The following table lists the flags in ControlFlags. For more information, see SECURITY_DESCRIPTOR_CONTROL.
        /// </summary>
        public ControlFlags ControlFlags
        {
            get { return (ControlFlags)Convert.ToUInt32(this[WmiProperties.ControlFlags] ?? 0); }
        }

        /// <summary>
        /// Each array entry defines the type of object access that the system grants to a specific user or group.
        /// </summary>
        public WmiAce[] DACL
        {
            get { return this[WmiProperties.DACL] as WmiAce[]; }
            set { this[WmiProperties.DACL] = value; }
        }

        /// <summary>
        /// Group that owns this object.
        /// </summary>
        public WmiTrustee Group
        {
            get { return this[WmiProperties.Group] as WmiTrustee; }
            set { this[WmiProperties.Group] = value; }
        }

        /// <summary>
        /// Owner of an object.
        /// </summary>
        public WmiTrustee Owner
        {
            get { return this[WmiProperties.Owner] as WmiTrustee; }
            set { this[WmiProperties.Owner] = value; }
        }

        /// <summary>
        /// Each array entry defines the type of access attempts that generate audit records for a specific user or group.
        /// </summary>
        public WmiAce[] SACL
        {
            get { return this[WmiProperties.SACL] as WmiAce[]; }
            set { this[WmiProperties.SACL] = value; }
        }
    }
}
